<template>
  <h2>toRaw和markRaw</h2>
  <h2>state:{{ state }}</h2>
  <button @click="testToRaw">测试1</button>
  <button @click="testmarkRaw">测试2</button>
</template>

<script lang="ts">
interface UserInfo {
  name: string
  age: number
  likes?: Array<string>
}
import { defineComponent, reactive, toRaw, markRaw } from 'vue'

export default defineComponent({
  name: 'App',
  setup() {
    const state = reactive<UserInfo>({
      name: '小明',
      age: 20
    })
    const testToRaw = () => {
      const user = toRaw(state)
      user.name += '2222'
    }
    const testmarkRaw = () => {
      const likes = ['a', 'b']
      // markRaw标记的对象数据,从此以后都不能再成为代理对象了
      state.likes = markRaw(likes)
      console.log(state)

      setInterval(() => {
        if (state.likes) {
          state.likes[0] += '11111'
        }
        console.log(111)
      }, 1000)
    }
    return {
      state,
      testToRaw,
      testmarkRaw
    }
  }
})
</script>

<style scoped>
</style>